﻿using log4net;
using Mathmall.Administration.common;
using Mathmall.Administration.Service;
using Mathmall.Administration.Vo;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Unicode;
using Mathmall.Administration.Models;
using Microsoft.AspNetCore.Authorization;

namespace Mathmall.Administration.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class BrandController : ControllerBase
    {
        private static readonly ILog _logger = LogManager.GetLogger(typeof(LoginController));

        private readonly IBrandService _brandService;
        private readonly ICategoryBrandRelationService _categoryBrandRelationService;

        public BrandController(IBrandService brandService, ICategoryBrandRelationService categoryBrandRelationService)
        {
            _brandService = brandService;
            _categoryBrandRelationService = categoryBrandRelationService;
        }

        /// <summary>
        /// 保存品牌信息
        /// </summary>
        /// <param name="brandVo"></param>
        /// <returns></returns>
        [Authorize(Policy = "AddBrand")]
        [HttpPost("[action]")]
        public async Task<ActionResult<int>> AddBrand(BrandVo brandVo)
        {
            await _brandService.AddBrand(brandVo);
            return CreatedAtAction("AddBrand", null);
        }

        /// <summary>
        /// 根据BrandId获取品牌信息
        /// </summary>
        /// <param name="brandId"></param>
        /// <returns></returns>
        [Authorize(Policy = "QueryBrand")]
        [HttpGet("[action]")]
        public async Task<ActionResult<BrandVo>> QuerySingleByBrandId(int brandId)
        {
            BrandVo? brandVo = await _brandService.QuerySingleByBrandId(brandId);
            if (brandVo == null)
            {
                return NotFound();
            }

            return brandVo;
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="page"></param>
        /// <param name="pageSize"></param>
        /// <param name="condition">模糊查询条件</param>
        [Authorize(Policy = "QueryBrand")]
        [HttpGet("{page}/{pageSize}/{condition}")]
        public async Task<ActionResult<PaginatedList<BrandVo>>> PaginatedQuery(int page, int pageSize,
            string condition = "")
        {
            PaginatedList<BrandVo>? brandVos = await _brandService.PaginatedQuery(page, pageSize, condition);

            if (brandVos != null)
            {
                string v = JsonSerializer.Serialize(new R<PaginatedList<BrandVo>>().Success(brandVos),
                    new JsonSerializerOptions()
                    {
                        Encoder = JavaScriptEncoder.Create(UnicodeRanges.All)
                    });
                return brandVos;
            }

            return NoContent();
        }

        /// <summary>
        /// 更新品牌信息
        /// </summary>
        /// <param name="brandVo"></param>
        /// <returns></returns>
        [Authorize(Policy = "EditBrand")]
        [HttpPut("[action]")]
        public async Task<ActionResult<int>> UpdateBrand(BrandVo brandVo)
        {
            await _brandService.UpdateBrand(brandVo);
            return Ok();
        }

        /// <summary>
        /// 删除品牌
        /// </summary>
        /// <param name="brandIDs"></param>
        /// <returns></returns>
        [Authorize(Policy = "DeleteBrand")]
        [HttpDelete("[action]")]
        public async Task<ActionResult<int>> DeleteBrand(List<int> brandIDs)
        {
            await _brandService.DeleteBrand(brandIDs);
            return Ok();
        }

        /// <summary>
        /// 新增品牌和分类的关联关系
        /// </summary>
        /// <param name="categoryBrandRelationVo"></param>
        /// <returns></returns>
        [HttpPost("[action]")]
        public async Task<int> AddCategoryToBrand(CategoryBrandRelationVo categoryBrandRelationVo)
        {
            return await _categoryBrandRelationService.AddCategoryBrandRelationAsync(categoryBrandRelationVo);
        }

        /// <summary>
        /// 根据品牌id查询关联的分类
        /// </summary>
        /// <param name="brandID"></param>
        /// <returns></returns>
        [HttpGet("{brandID}")]
        public async Task<List<CategoryBrandRelationVo>> GetCategoryBrandRelation(int brandID)
        {
            return await _categoryBrandRelationService.GetCategoryBrandRelations(brandID);
        }

        /// <summary>
        /// 删除品牌与分类的关联
        /// </summary>
        /// <param name="categoryBrandRelationID"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<int> DeleteCategoryBrandRelation(int categoryBrandRelationID)
        {
            return await _categoryBrandRelationService.DeleteCategoryBrandRelation(categoryBrandRelationID);
        }
    }
}